<?php

class Materials_Model_Material_Storage_Table extends Workset_Model_Object_Storage_Table_Abstract {

	protected $_objectClass 		= 'Materials_Model_Material';

	protected $_tableClass 			= 'Materials_Table_Material';

    protected $_categoryObjectClass = 'Materials_Model_Category';

    protected $_categoryTableClass  = 'Materials_Table_Category';

	protected $_iteratorClass 		= 'Workset_Model_Object_Iterator';

	protected $_material2CategoryTableClass = 'Materials_Table_Material2Category';


    protected $_material2CatalogueCategoryTableClass = 'Materials_Table_Material2CatalogueCategory';

	protected $_catalogueCategoryTableClass = 'Catalogue_Table_Category';

	protected $_catalogueCategoryObjectClass = 'Catalogue_Model_Category';


    protected $_material2LabelTableClass = 'Materials_Table_Material2Label';

    protected $_labelTableClass = 'Materials_Table_Label';

    protected $_labelObjectClass = 'Materials_Model_Label';


    protected $_commentTableClass = 'Materials_Table_Comment';

    protected $_commentObjectClass = 'Materials_Model_Comment';
    

    protected $_searchContentTable = 'Materials_Table_SearchContent';

    protected $_visitorsTable = 'Materials_Table_Visitor';

    function getCategories(Workset_Model_Object_Interface $object) {
        $row = $object->getData();
		if ($row instanceof Workset_Db_Table_Row) {
			$rowset = $row->findManyToManyRowset($this->_categoryTableClass, $this->_material2CategoryTableClass, 'material');

			return $this->getIterator($rowset, array(
                'objectClass' => $this->_categoryObjectClass,
                'storage' => null
            ));
		}
    }

    function save(Workset_Model_Object_Interface $object, $data = null) {
        if (!$object->getId()) {
            if (null !== $data && isset($data['created'])) {
                $date = (int)$data['created'];
            } else {
                $date = (int)$object['created'];
            }

            if ($date <= 0) {
                $date = time();
            }

            $object['created'] = $date;
        }

        return parent::save($object, $data);
    }

	function linkCatalogueCategory(Workset_Model_Object_Interface $object,
								  Workset_Model_Object_Interface $category)
	{
		$row = $category->getData();
		if ($row instanceof Workset_Db_Table_Row) {

			$table = $row->getTable();

			$depTables = $table->getDependentTables();
			if (!in_array($this->_material2CatalogueCategoryTableClass, $depTables)) {
				$depTables[] = $this->_material2CatalogueCategoryTableClass;
				$table->setDependentTables($depTables);
			}

			return $row->addDependentRow($this->_material2CatalogueCategoryTableClass, array(
				'material_id' => $object->getId(),
			));
		}
	}

	function unlinkCatalogueCategory(Workset_Model_Object_Interface $object,
									Workset_Model_Object_Interface $category)
	{
		$row = $category->getData();
        if ($row instanceof Workset_Db_Table_Row) {
			$table = $row->getTable();

			$depTables = $table->getDependentTables();
			if (!in_array($this->_material2CatalogueCategoryTableClass, $depTables)) {
				$depTables[] = $this->_material2CatalogueCategoryTableClass;
				$table->setDependentTables($depTables);
			}

            $subrow = $row->findDependentRow($this->_material2CatalogueCategoryTableClass, 'category', array(
                'material_id = ?' => $object->getId(),
            ));
            if ($subrow) {
                return $subrow->delete();
            }
        }
	}

	function getCatalogueCategories(Workset_Model_Object_Interface $object) {
		$row = $object->getData();
        if ($row instanceof Workset_Db_Table_Row) {
			$table = $row->getTable();

			$depTables = $table->getDependentTables();
			if (!in_array($this->_material2CatalogueCategoryTableClass, $depTables)) {
				$depTables[] = $this->_material2CatalogueCategoryTableClass;
				$table->setDependentTables($depTables);
			}

			$rowset = $row->findManyToManyRowset($this->_catalogueCategoryTableClass, $this->_material2CatalogueCategoryTableClass, 'material');

			return $this->getIterator($rowset, array(
                'objectClass' => $this->_catalogueCategoryObjectClass,
                'storage' => null
            ));
		}
	}

    function addSearchContent(Workset_Model_Object_Interface $object, $content) {
        $content['material_id'] = $object->getId();

        $table = $this->_getTable($this->_searchContentTable);
        return $table->insert($content);
    }

    function updateSearchContent(Workset_Model_Object_Interface $object, $content) {
        $table = $this->_getTable($this->_searchContentTable);

        $select = $table->select();

        $select->from(
            $table->info(Zend_Db_Table_Abstract::NAME),
            array('material_id')
        );

        $select->where('material_id = ?', $object->getId());

        $result = $table->getAdapter()->fetchCol($select);

        if (!$result) {
            return $this->addSearchContent($object, $content);
        } else {
            return $table->update($content, array('material_id = ?' => $object->getId()));
        }

    }

    function deleteSearchContent(Workset_Model_Object_Interface $object) {
        $table = $this->_getTable($this->_searchContentTable);
        return $table->delete(array('material_id = ?' => $object->getId()));
    }

    function getLabels(Workset_Model_Object_Interface $object) {
        $row = $object->getData();
		if ($row instanceof Workset_Db_Table_Row) {
			$rowset = $row->findManyToManyRowset($this->_labelTableClass, $this->_material2LabelTableClass, 'material');

			return $this->getIterator($rowset, array(
                'objectClass' => $this->_labelObjectClass,
                'storage' => null
            ));
        }
    }

    function updateCommentsStatus(Workset_Model_Object_Interface $object, $status) {
        $table = $this->_getTable($this->_commentTableClass);

        return $table->update(
            array('status' => (int)$status),
            array('material_id = ?' => $object->getId())
        );
    }
}
